package com.csun.nusing.ui.fragment;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.csun.nusing.presenter.BasePresenter;
import com.csun.nusing.ui.IView;
import com.csun.nusing.util.SPUtils;

/**
 * BaseFragment 实现Iview ClassName: BaseFragment
 * 
 * @Description: TODO
 * @author fzq
 * @date 2017-10-18
 */
public class BaseFragment<T extends BasePresenter> extends Fragment implements
		IView {

	protected T mPresenter;
	private Float scale;

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		initData();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View containerView = inflater.inflate(getContentViewId(), container,
				false);
		initPresenter();
		initSubViews(containerView);
		initFontScale();
		return containerView;
	}

	// 0.85 小, 1 标准大小, 1.15 大，1.3 超大 ，1.45 特大
	private void initFontScale() {
		scale = (Float) SPUtils.get(getActivity(), "SCALE", (float)1.0);
		Configuration configuration = getResources().getConfiguration();
		configuration.fontScale = scale;
		// 0.85 小, 1 标准大小, 1.15 大，1.3 超大 ，1.45 特大
		DisplayMetrics metrics = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay()
				.getMetrics(metrics);
		metrics.scaledDensity = configuration.fontScale * metrics.density;
		getActivity().getResources()
				.updateConfiguration(configuration, metrics);
	}

	private void initPresenter() {
		if (mPresenter == null)
			mPresenter = getPresenter();
	}

	protected T getPresenter() {
		return null;
	}

	@Override
	public ViewGroup getViewGroupRoot() {
		return null;
	}

	@Override
	public int getContentViewId() {
		return 0;
	}

	@Override
	public void initSubViews(View view) {

	}

	@Override
	public void initData() {

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (mPresenter != null) {
			mPresenter.detachViewRef();
		}
	}

}
